package com.itheima.blog.article.task;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.itheima.blog.article.entity.TBlog;
import com.itheima.blog.article.entity.TComment;
import com.itheima.blog.article.mapper.TBlogMapper;
import com.itheima.blog.article.service.ITBlogService;
import com.itheima.blog.article.service.ITCommentService;
import com.itheima.common.utills.ObjectUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.itheima.common.constants.Constant.BLOG_ADD_KEY;
import static com.itheima.common.constants.Constant.BLOG_CLICK_KEY;

@Component
@RequiredArgsConstructor
public class TimingServiceTask/* implements ApplicationContextAware */{
/*    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }*/

    private final ITBlogService blogService;
    private final StringRedisTemplate redisTemplate;
    @Scheduled(cron = "0 */1 * * * ?")
    @Transactional
    public void heatTask(){
        LocalDateTime time = LocalDateTime.now().minusDays(1);
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(BLOG_CLICK_KEY);
        List<String> list=new ArrayList<>();
        entries.forEach((k,v)-> list.add(k.toString()));

        List<Long> longs = list.stream().map(Long::valueOf).collect(Collectors.toList());
        if (longs.size() == 0)
            return;
        List<TBlog> tBlogs = blogService.lambdaQuery().in(TBlog::getId, longs).list();
        List<TBlog> tBlogList=new ArrayList<>();
        for (TBlog tBlog : tBlogs) {
            Long tBlogId = tBlog.getId();
            Long commentCount = redisTemplate.opsForHash().increment(BLOG_ADD_KEY,tBlogId.toString(),0);
            Long clickCount = redisTemplate.opsForHash().increment(BLOG_CLICK_KEY, tBlogId.toString(), 0);
            Long heat=commentCount*7+clickCount*3;
            tBlog.setId(tBlogId);
            tBlog.setHeat(tBlog.getHeat()+heat);
            Integer i=(int)clickCount.longValue();
            tBlog.setClickCount(tBlog.getClickCount()+i);
            tBlogList.add(tBlog);
        }

        blogService.updateBatchById(tBlogList);
        for (Long aLong : longs) {
            redisTemplate.opsForHash().put(BLOG_CLICK_KEY,String.valueOf(aLong),"0");
        }
    }

}
